package io.renren.modules.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.R;
import io.renren.modules.work.entity.*;
import io.renren.modules.work.service.ScExtrasdtlService;
import io.renren.modules.work.service.ScProjectService;
import io.renren.vo.ExpenseVo;
import io.renren.vo.ScExtrasVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.work.dao.ScExtrasDao;
import io.renren.modules.work.service.ScExtrasService;
import org.springframework.transaction.annotation.Transactional;


@Service("scExtrasService")
public class ScExtrasServiceImpl extends ServiceImpl<ScExtrasDao, ScExtrasEntity> implements ScExtrasService {
    @Autowired
    private ScExtrasdtlService scExtrasdtlService;

    @Autowired
    private ScProjectService scProjectService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //时间判断
        String sTime = null;
        String eTime = null;
        if(StringUtils.isNotBlank((String) params.get("sTime"))&&StringUtils.isNotBlank((String) params.get("eTime"))){
            sTime = sdf.format( Long.parseLong((String) params.get("sTime")));
            eTime = sdf.format( Long.parseLong((String) params.get("eTime")));
        }

        IPage<ScExtrasEntity> page = baseMapper.selectExtrasList(new Query<ScExtrasEntity>().getPage(params),
                (String) params.get("projectid"),
                (String) params.get("extrasName"),
                (String) params.get("cretimeSort"),
                (String) params.get("priceSort"),
                sTime,
                eTime);

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public R mSave(String params) {
        ScExtrasVo scExtrasVo = JSONObject.parseObject(params, ScExtrasVo.class);
        ScExtrasEntity scExtrasEntity = new ScExtrasEntity();
        String id = ConfigConstant.getId();
        scExtrasEntity.setExtrasId(id);
        scExtrasEntity.setExtrasNo(ConfigConstant.generateExtrasNo());
        scExtrasEntity.setExtrasName(scExtrasVo.getExtrasName());
        scExtrasEntity.setCretime(new Date());
        scExtrasEntity.setPrice(scExtrasVo.getPrice());
        scExtrasEntity.setMemo(scExtrasVo.getMemo());
        scExtrasEntity.setProjectid(scExtrasVo.getProjectid());
        ScProjectEntity scproject = scProjectService.getById(scExtrasVo.getProjectid());
        scExtrasEntity.setProjectname(scproject.getProjectname());
        scExtrasEntity.setCommitMan(scExtrasVo.getCommitMan());
        baseMapper.insert(scExtrasEntity);
        List<ScExtrasdtlEntity> scExtrasdtl = scExtrasVo.getScExtrasdtl();
        if(scExtrasdtl!=null){
            for (ScExtrasdtlEntity scExtrasdtlEntity : scExtrasdtl) {
                ScExtrasdtlEntity dtl = new ScExtrasdtlEntity();
                dtl.setExtrasId(id);
                dtl.setExtrasdtlId(ConfigConstant.getId());
                dtl.setMemo(scExtrasdtlEntity.getMemo());
                dtl.setPrice(scExtrasdtlEntity.getPrice());
                dtl.setDate(scExtrasdtlEntity.getDate());
                dtl.setExtrasdtlNo(ConfigConstant.generateExtrasDtlNo());
                dtl.setExtrasdtlName(scExtrasdtlEntity.getExtrasdtlName());
                scExtrasdtlService.save(dtl);
            }
        }
        return R.ok();
    }
    @Transactional
    @Override
    public R mUpdate(String params) {
        ScExtrasVo scExtrasVo = JSONObject.parseObject(params, ScExtrasVo.class);
        ScExtrasEntity scExtrasEntity = baseMapper.selectById(scExtrasVo.getExtrasId());
        scExtrasEntity.setExtrasName(scExtrasVo.getExtrasName());
        scExtrasEntity.setPrice(scExtrasVo.getPrice());
        scExtrasEntity.setCommitMan(scExtrasVo.getCommitMan());
        scExtrasEntity.setMemo(scExtrasVo.getMemo());
        baseMapper.updateById(scExtrasEntity);
        List<ScExtrasdtlEntity> scExtrasdtl = scExtrasVo.getScExtrasdtl();
        //删除列表中不存在的细单
        List<String> ids = new ArrayList<>();
        for (ScExtrasdtlEntity scExtrasdtlEntity : scExtrasdtl) {
            if(scExtrasdtlEntity.getExtrasdtlId()!=null){
                ids.add(scExtrasdtlEntity.getExtrasdtlId());
            }
        }
        QueryWrapper<ScExtrasdtlEntity> scExtrasdtlEntityQueryWrapper = new QueryWrapper<>();
        scExtrasdtlEntityQueryWrapper.eq("extras_id",scExtrasEntity.getExtrasId());
        if(ids.size()>0){
            scExtrasdtlEntityQueryWrapper.notIn("extrasdtl_id",ids);
        }
        scExtrasdtlService.remove(scExtrasdtlEntityQueryWrapper);
        for (ScExtrasdtlEntity scExtrasdtlEntity : scExtrasdtl) {
            if(StringUtils.isNotBlank(scExtrasdtlEntity.getExtrasdtlId())){
                scExtrasdtlService.updateById(scExtrasdtlEntity);
            }else{
                scExtrasdtlEntity.setExtrasId(scExtrasVo.getExtrasId());
                scExtrasdtlService.save(scExtrasdtlEntity);

            }
        }
        return R.ok();
    }

    @Override
    public R mInfo(String extrasId) {

        ScExtrasEntity scExtrasEntity = baseMapper.selectById(extrasId);
        ScExtrasVo scExtrasVo = new ScExtrasVo();
        BeanUtil.copyProperties(scExtrasEntity,scExtrasVo);
        QueryWrapper<ScExtrasdtlEntity> scExtrasdtlEntityQueryWrapper = new QueryWrapper<>();
        scExtrasdtlEntityQueryWrapper.eq("extras_id",extrasId);
        List<ScExtrasdtlEntity> list = scExtrasdtlService.list(scExtrasdtlEntityQueryWrapper);
        scExtrasVo.setScExtrasdtl(list);
        return R.ok().put("data",scExtrasVo);
    }

}